<template>
  <div class="about">
    <div :style="imgStyle">
      <img src="images/login/bg2.png">
      <el-card style="width: 300px;height: 400px;border-radius: 25px;
    background-color: rgb(255,255,255);position: absolute;overflow: hidden;
    padding: 0px 50px;margin: 0 auto;
    top: 200px;right: 200px;box-shadow: rgb(59, 180, 74) 0px 0px 30px 0px;">
        <img style="top: 20px;left: 150px;width: 90px;height: 80px;" src="images/login/bg3.png">
        <div style="margin-top: 90px;margin-bottom: 6px;font-size: 34px;font-weight: 600">物管系统</div>
        <!--label-width设置文本所占的宽度如果不设置,文本单独显示一行-->
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm"
                 label-width="80px" style="width: 100%;margin: 30px auto">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password"></el-input>
          </el-form-item>
          <el-form-item>
            <!--让某一个元素位置进行微调时 使用相对定位-->
            <el-button style="position: relative;right: 30px"
                       type="primary" @click="submitForm('ruleForm')">登录</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: 'root',
        password: '123456'
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
      },
      imgStyle:{
        width:"100%",
        height:"1280px",
        backgroundImage:"url("+require('../../public/images/login/bg.png')+")",
        backgroundSize:"cover"
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "http://localhost:30006/users/login";
          console.log(url);
          let formData = this.qs.stringify(this.ruleForm);
          this.axios.post(url,formData).then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000){
              this.$message({
                showClose: true,
                message: '登录成功!',
                type: 'success'
              });
              let jwt = responseBody.data;
              console.log('jwt = ' + jwt);
              localStorage.setItem('jwt', jwt);
              console.log('已经将JWT存入到localStorage中，即将跳转页面……');
              this.$router.push('/')
            }else {
              this.$message({
                showClose: true,
                message: responseBody.message,
                type: 'error'
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  }
}
</script>

<style>
.about{
  /*background-image: url("images/login/bg.png");*/
  margin: 0;  /*去掉自带的外边距*/
  /*cover封面  把背景图片作为封面*/
  background-size: cover;
  /*禁止重复*/
  background-repeat: no-repeat;
  text-align: center;
}
h1{
  font-size: 72px;
  color: #0096dc;
  margin-bottom: 0;
}
img{
  width: 43.9%;
  position: absolute;
  top: 150px;
  left: 200px;
}
h2{
  font-size: 32px;color: #0096dc;
  margin-bottom: 0;
}
</style>